package by.amakarevich.webapp.page.edit.thread;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.Thread;
import by.amakarevich.datamodel.Product;
import by.amakarevich.services.ThreadService;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.home.HomePage;


public class ThreadCreateEditPage extends AlternativeBaseLayout {

	@Inject
	private ThreadService threadService;
	
	public ThreadCreateEditPage(final Thread thread, final Product product) {
		super();
		Form<Thread> form = new Form<Thread>("form", new CompoundPropertyModel<Thread>(thread));

		final TextField<String> tfColor = new TextField<String>("color");
		tfColor.add(new PropertyValidator<String>());
		tfColor.setLabel(new ResourceModel("p.product.color"));
		form.add(tfColor);
		
		form.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				if(thread.getId()==null){
					threadService.createNewThread(thread, product);
				}else{
					threadService.updateThread(thread);
				}
					
				HomePage page = new HomePage();


				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);

	}
		

}
